package flc.ast.adapter;

import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class TypeMovieAdapter extends StkProviderMultiAdapter<StkResBean> {

    public TypeMovieAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new TypeMovieProvider());
    }

    private class TypeMovieProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_type_movie;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {

            Glide.with(baseViewHolder.itemView).load(bean.getThumbUrl())
                    .into((ImageView) baseViewHolder.getView(R.id.ivTypeMovieItemImg));
            baseViewHolder.setText(R.id.tvTypeMovieItemName, bean.getName());

            String str = new Gson().toJson(bean.getExtra());
            String actor = getContext().getString(R.string.actor_text2);

            try {
                JSONObject extra = new JSONObject(str);
                JSONArray actorList = extra.getJSONArray("actorList");
                baseViewHolder.setText(R.id.tvTypeMovieItemDate, extra.optString("releaseDate"));
                for (int i = 0; i < actorList.length(); i++) {
                    JSONObject item = actorList.getJSONObject(i);
                    if (!item.getString("role").equals("导演")) {
                        actor += item.getString("name") + " / ";
                    }
                }
                baseViewHolder.setText(R.id.tvTypeMovieItemActor, actor);
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

            String tag = "";
            for (String s : bean.getTagNameList()) {
                tag += s + " / ";
            }
            baseViewHolder.setText(R.id.tvTypeMovieItemType, tag);

            //评分
            double score = (double) bean.getScore_total() / (bean.getScore_count() == 0 ? 1 : bean.getScore_count());
            String formattedNumber = String.format("%.1f", score);
            baseViewHolder.setText(R.id.tvTypeMovieItemScore, formattedNumber);
        }
    }

}
